//
//  OrderListCell.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/15.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
// 绘制未处理订单的Cell
//

#import "OrderListCell.h"
#import <QuartzCore/QuartzCore.h>//</quartzcore>

@interface OrderListCell(){
    UIView  *_orderDetailView;
    NSUInteger _detailViewHeight;
    UILabel *_addressLabel;
    UILabel *_requireTimeLabel;
    UILabel *_paymentLabel;
    UILabel *_totalCountLabel;
    UILabel *_moneyLabel;
    UILabel *_timeLabel;
    UIView *_spacingView;
    NSUInteger _merchandisePriceWidth;
    UIView *_line1View;
    UIImageView *_line2View;
    UIView *_line3View;
}
@end

@implementation OrderListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier frame:(CGRect)frame{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier frame:frame];
    if (self) {
        [self initSubViewWithFrame:frame];
    }
    return self;
}

-(void)initSubViewWithFrame: (CGRect) frame{
    
    CGSize priceSpacingSize = [@"¥ 1000.0 X 99" sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    _merchandisePriceWidth = priceSpacingSize.width + kOrderCellSpacing * 2;
    
    _spacingView = [[UIView alloc] init];
    _orderDetailView = [[UIView alloc] init];
    [_spacingView setBackgroundColor:PanelBgColorDefault];
    [self addSubview:_spacingView];
    [self addSubview:_orderDetailView];
    
    _addressLabel = [[UILabel alloc] init];
    _addressLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _addressLabel.textColor = TextColorDefault;
    [self addSubview:_addressLabel];
    
    _requireTimeLabel = [[UILabel alloc] init];
    _requireTimeLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _requireTimeLabel.textColor = TextColorImportant;
    [self addSubview:_requireTimeLabel];
    
    _paymentLabel = [[UILabel alloc] init];
    _paymentLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _paymentLabel.textColor = TextColorDefault;
    [self addSubview:_paymentLabel];
    
    _totalCountLabel = [[UILabel alloc] init];
    _totalCountLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
    _totalCountLabel.textColor = TextColorLight;
    [self addSubview:_totalCountLabel];
    
    _moneyLabel = [[UILabel alloc] init];
    _moneyLabel.font = [UIFont systemFontOfSize:TextFontSizeBH1];
    _moneyLabel.textColor = TextColorImportant;
    [self addSubview:_moneyLabel];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = [UIFont systemFontOfSize:TextFontSizeH5];
    _timeLabel.textColor = TextColorLight;
    [self addSubview:_timeLabel];
    
    _line1View = [[UIView alloc] init];
    _line1View.backgroundColor = LineColorDefault ;
    [self addSubview: _line1View];
    
    _line2View = [[UIImageView alloc] init];
    [self addSubview: _line2View];
    
    _line3View = [[UIView alloc] init];
    _line3View.backgroundColor = LineColorDefault ;
    [self addSubview: _line3View];
}

-(void)drawOrderDetailView:(JFBOrderDS *)order{
    for( UIView *v in _orderDetailView.subviews) {
        [v removeFromSuperview];
    }
    NSUInteger height = kOrderCellHeaderHeight;
    NSUInteger width = self.bounds.size.width - kOrderCellSpacing * 2 - _merchandisePriceWidth;
    
    for( JFBMerchandiseDS *ds in order.merchandiseList ) {
        

        
        UILabel *label = [[UILabel alloc] init];
        label.lineBreakMode = NSLineBreakByCharWrapping;
        label.numberOfLines = 0;
        [label setTextColor:TextColorLight];
        label.font = [UIFont systemFontOfSize:TextFontSizeH4];
        label.text = ds.name;
        
        CGSize size = [label sizeThatFits:CGSizeMake(width, MAXFLOAT)];
        
        CGFloat lableX= 0;
//        if (ds.type == 1)
//        {
//            UILabel *lblZengPin = [[UILabel alloc] initWithFrame:CGRectMake(kOrderCellSpacing, height + kOrderCellSpacing, 35, size.height)];
//            [lblZengPin setTextColor: [UIColor whiteColor]];
//            [lblZengPin setTextAlignment:NSTextAlignmentCenter];
//            [lblZengPin setText:@"赠品"];
//            [lblZengPin setFont:[UIFont systemFontOfSize:13]];
//            [lblZengPin setBackgroundColor: [UIColor redColor]];
//            [_orderDetailView addSubview:lblZengPin];
//            lableX = CGRectGetMaxX( lblZengPin.frame);
//        }
        
        label.frame =CGRectMake(lableX + kOrderCellSpacing, height + kOrderCellSpacing, width, size.height);
        
        [_orderDetailView addSubview:label];
        
        UILabel *priceLabel = [[UILabel alloc] init];
        NSString *price = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:ds.marketPrice]];
        priceLabel.font = [UIFont systemFontOfSize:TextFontSizeH3];
        priceLabel.textColor = TextColorLight;
        CGSize priceSize = [price sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
        CGFloat priceX = self.bounds.size.width - kOrderCellSpacing - priceSize.width;
        CGFloat priceY = label.frame.origin.y + (label.bounds.size.height - priceSize.height) / 2;
        CGRect priceRect = CGRectMake(priceX, priceY, priceSize.width, priceSize.height);
        priceLabel.frame = priceRect;
        priceLabel.text = price;
        
        [_orderDetailView addSubview:priceLabel];
        
        height += size.height + kOrderCellSpacing;
        
    }
    _detailViewHeight = height - kOrderCellHeaderHeight + kOrderCellSpacing;
}

-(void)setOrder:(JFBOrderDS *)order{
    CGSize addressSize = [order.address sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
     CGFloat addressX = kOrderCellMargin, addressY = (kOrderCellHeaderHeight - addressSize.height) / 2;

    CGRect addressRect = CGRectMake(addressX, addressY, self.bounds.size.width - kOrderCellSpacing * 2, addressSize.height);
    _addressLabel.text = order.address;
    _addressLabel.frame = addressRect;
    
    _line1View.frame = CGRectMake(kOrderCellSpacing, kOrderCellHeaderHeight, self.bounds.size.width - kOrderCellSpacing * 2, 1);
    
    CGSize requireTimeSize = [order.requireTime sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat requireTimeY = (kOrderCellHeaderHeight - requireTimeSize.height) / 2;
    CGFloat requireTimeX = self.bounds.size.width - kOrderCellMargin - requireTimeSize.width;
    CGRect requireTimeRect = CGRectMake(requireTimeX, requireTimeY, requireTimeSize.width, requireTimeSize.height);
    _requireTimeLabel.text = order.requireTime;
    _requireTimeLabel.frame = requireTimeRect;
    
    //计算底部空白间距
    NSString *money = [NSString stringWithFormat:@"¥ %@", [NSString decimalToString:order.money]];
    CGSize moneySize = [money sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeBH1]}];
    CGSize timeSize = [order.createAt sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH5]}];
    NSUInteger bottomSpacing = (kOrderCellBottomHeight - moneySize.height - timeSize.height) / 3;
    [self drawOrderDetailView:order];
 
    _line2View.frame = CGRectMake((self.bounds.size.width - _merchandisePriceWidth), kOrderCellHeaderHeight + kOrderCellSpacing, 2, _detailViewHeight - kOrderCellSpacing * 2);
    [self.drawLineHelper drawDashLineWithImageView:_line2View withColor: LineColorDefault];
    
    _line3View.frame = CGRectMake(kOrderCellSpacing, kOrderCellHeaderHeight + _detailViewHeight, self.bounds.size.width - kOrderCellSpacing * 2, 1);
    
    CGFloat moneyY = kOrderCellHeaderHeight + _detailViewHeight + bottomSpacing + 2;
    CGFloat moneyX = self.bounds.size.width - kOrderCellMargin - moneySize.width;
    CGRect moneyRect = CGRectMake(moneyX, moneyY, moneySize.width, moneySize.height);
    _moneyLabel.text = money;
    _moneyLabel.frame = moneyRect;
    
    CGSize paymentSize = [order.payment sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat paymentX = kOrderCellMargin, paymentY = moneyY + (moneySize.height - paymentSize.height) / 2;
    CGRect paymentRect = CGRectMake(paymentX, paymentY, paymentSize.width, paymentSize.height);
    _paymentLabel.text = order.payment;
    _paymentLabel.frame = paymentRect;
    
    NSString *totalCount = [NSString stringWithFormat:@"共%lu件", (unsigned long)order.totalCount];
    CGSize totalCountSize = [totalCount sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:TextFontSizeH3]}];
    CGFloat totalCountX = moneyX - kOrderCellSpacing / 2 - totalCountSize.width, totalCountY = moneyY + (moneySize.height - totalCountSize.height) / 2;
    CGRect totalCountRect = CGRectMake(totalCountX, totalCountY, totalCountSize.width, totalCountSize.height);
    _totalCountLabel.text = totalCount;
    _totalCountLabel.frame = totalCountRect;
    
    CGFloat timeY = moneyY + moneyRect.size.height + bottomSpacing - 2;
    CGFloat timeX =self.bounds.size.width - kOrderCellMargin - timeSize.width;
    CGRect timeRect = CGRectMake(timeX, timeY, timeSize.width, timeSize.height);
    _timeLabel.text = order.createAt;
    _timeLabel.frame = timeRect;
    
    _spacingView.frame = CGRectMake(0, kOrderCellHeaderHeight + kOrderCellBottomHeight + _detailViewHeight, self.bounds.size.width, kOrderCellSpacing);
    
    //设置Cell高度
    self.height = CGRectGetMaxY(_spacingView.frame);
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
